package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/models/mysql"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 药品添加
func (l *GoodAddLogic) GoodAdd(in *doctorrpc.GoodAddRequest) (*doctorrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line

	var doctorfind mysql.Doctor

	if err := doctorfind.DoctorFindById(mysql.DB, in.DoctorId); err != nil {
		return nil, errors.New("该医生不存在")
	}

	good := mysql.Good{
		GoodName:    in.GoodName,
		GoodAddress: in.GoodAddress,
		GoodNum:     in.GoodNum,
		DoctorId:    in.DoctorId,
	}

	if err := good.GoodAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &doctorrpc.GoodAddResponse{
		GoodId: int64(good.ID),
	}, nil
}
